/**
 * User: alesj
 * Date: 2004.9.28
 * Time: 15:39:18
 */

package com.generalynx.ecos.web.object;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.HashMap;
import java.util.Map;

/**
 * (Currently) Holds a track of all users signed in.
 */
public class AppStatusHolder implements HttpSessionBindingListener {

    protected static final Log logger = LogFactory.getLog(AppStatusHolder.class);
    private static Map c_infos = new HashMap();
    private InfoWrapper m_info;

    public AppStatusHolder(InfoWrapper info) {
        m_info = info;
    }

    private void status() {
        logger.debug("Users in application: " + infos().size());
    }

    public void valueBound(HttpSessionBindingEvent event) {
        status();
        logger.debug("Binding info: " + getInfo());
        c_infos.put(event.getSession(), getInfo());
    }

    public void valueUnbound(HttpSessionBindingEvent event) {
        logger.debug("Unbinding info: " + getInfo());
        c_infos.remove(event.getSession());
        status();
    }

    public static Map infos() {
        return c_infos;
    }

    public InfoWrapper getInfo() {
        return m_info;
    }
}
